package com.yanqu.road.server.gameplayer.module.player;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.log.LogLiveShowTime;
import com.yanqu.road.entity.player.UserDailyData;
import com.yanqu.road.logic.bussiness.player.DailyBussiness;
import com.yanqu.road.pb.activity.CardProto;
import com.yanqu.road.pb.player.DailyDataProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.pb.ActivityPb;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.LocalDateTimeHelper;

import java.util.Date;

public class DailyDataModule extends GeneralModule {
    private UserDailyData userDailyData;
    public DailyDataModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        userDailyData = DailyBussiness.getUserDailyData(player.getUserId());
        initData();
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if(null != userDailyData){
            if(userDailyData.isInsertOption()){
                DailyBussiness.addUserDailyData(userDailyData);
            }else if(userDailyData.isUpdateOption()){
                DailyBussiness.updateUserDailyData(userDailyData);
            }
        }
        return true;
    }

    @Override
    public void afterLogin() {
        syncDailyLiveShowTime();
        syncWeekCardDailyData();
    }

    private void initData(){
        if(null == userDailyData){
            UserDailyData tempData = new UserDailyData();
            tempData.setUserId(player.getUserId());
            tempData.setResetTime(LocalDateTimeHelper.getZeroTimeTimeStamp() / 1000);
            tempData.resetValue();
            tempData.setInsertOption();
            userDailyData = tempData;
        }
    }

    public void resetOneDay(){
        if(null == userDailyData){
            return;
        }
        long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp() / 1000;
        boolean isReset = false;
        if(zeroTime > userDailyData.getResetTime()) {
            synchronized (userDailyData) {
                if(zeroTime > userDailyData.getResetTime()) {
                    userDailyData.resetValue();
                    userDailyData.setResetTime(zeroTime);
                    isReset = true;
                }
            }
        }
        if(isReset){
            syncDailyLiveShowTime();
            syncWeekCardDailyData();
        }
    }

    public void addLiveShowTime(long time){
        if(null != userDailyData){
            synchronized (userDailyData){
                resetOneDay();
                userDailyData.setLiveShowTime(userDailyData.getLiveShowTime() + time);
            }
        }
    }

    public long getLiveShowTime(){
        if(null == userDailyData){
            return 0;
        }
        return userDailyData.getLiveShowTime();
    }

    public long getWeekCardTrystTimes() {
        if(null == userDailyData){
            return 0;
        }
        return userDailyData.getWeekCardTrystTimes();
    }

    public void addWeekCardTrystTimes(long times) {
        if(null != userDailyData){
            synchronized (userDailyData){
                resetOneDay();
                userDailyData.setWeekCardTrystTimes(userDailyData.getWeekCardTrystTimes() + times);
            }
        }
    }

    public long getWeekCardTravelTimes() {
        if(null == userDailyData){
            return 0;
        }
        return userDailyData.getWeekCardTravelTimes();
    }

    public void addWeekCardTravelTimes(long times) {
        if(null != userDailyData){
            synchronized (userDailyData){
                resetOneDay();
                userDailyData.setWeekCardTravelTimes(userDailyData.getWeekCardTravelTimes() + times);
            }
        }
    }

    /**
     * 描述：计算直播时间
     * 作者：zrq
     * 时间：2022/12/1
     * 参数：

     * 返回值：
     **/
    public synchronized void calcLiveShowTime(boolean isResetLiveShowTime, boolean isCloseLiveShow){
        if(isResetLiveShowTime){
            resetOneDay();
        }else {
            if (player.isLiveShow()) {//开播状态
                long nowTime = System.currentTimeMillis();
                long liveShowTime = nowTime - player.getLastCalcLiveShowTime();
                addLiveShowTime(liveShowTime);
                long dayLiveShowTime = getLiveShowTime();
                notifyLiveShow(liveShowTime, dayLiveShowTime);
                player.setLastCalcLiveShowTime(nowTime);
                if (isCloseLiveShow) {
                    logLiveShowTime();
                    player.setLiveShow(false);
                    player.setLiveShowStartTime(0);
                    player.setLastCalcLiveShowTime(0);
                }
            }
        }
    }

    public void notifyLiveShow(long liveShowTime, long dayLiveShowTime){
        player.notifyListener(eGamePlayerEventType.LiveShowTime.getValue(), liveShowTime);
        player.notifyListener(eGamePlayerEventType.LiveShowDailyTime.getValue(), dayLiveShowTime);
        player.notifyListener(eGamePlayerEventType.LiveShowDay.getValue(), dayLiveShowTime);
    }

    public void notifyLiveShowWashe(){
        if(player.isLiveShow()){
            player.notifyListener(eGamePlayerEventType.LiveShowWasheWinTimes.getValue(), 1);
        }
    }

    public void logLiveShowTime(){
        AutoLogMgr.add(new LogLiveShowTime(player.getUserId(), new Date(player.getLiveShowStartTime()), System.currentTimeMillis() - player.getLiveShowStartTime()));
    }

    public void syncDailyLiveShowTime(){
        if(null != userDailyData) {
            DailyDataProto.DailyLiveShowTimeSyncMsg.Builder syncMsg = DailyDataProto.DailyLiveShowTimeSyncMsg.newBuilder();
            syncMsg.setDailyLiveShowTime(userDailyData.getLiveShowTime());
            player.sendPacket(Protocol.U_DAILY_LIVE_SHOW_TIME_SYNC, syncMsg);
        }
    }

    public void syncWeekCardDailyData(){
        CardProto.WeekCardDailyDataSyncMsg.Builder syncMsg = ActivityPb.parseWeekCardDailyDataSyncMsg(userDailyData);
        player.sendPacket(Protocol.U_DAILY_WEEK_CARD_DATA_SYNC, syncMsg);
    }
}
